Back

Create a bare metal k8s cluster using kubeadm

Head node

  1. Spin up VM instance (add to network, router interfaces, ssh key etc.). This will be for the head node.

  2. Add instance IP to /etc/hosts on DMZ.

  3. SSH to instance and add new user. Add user to group sudo.

     $ usermod -aG sudo <username>
     $ sudo visudo
    
     # Allow members of group sudo to execute any command
     %sudo   ALL=(ALL:ALL) NOPASSWD:ALL
    
     $ sudo usermod -aG sudo <username>
  4. Add new user's public ssh keys.

     $ sudo su - <username>
     $ mkdir .ssh
     $ vim .ssh/authorized_keys
     $ chmod 600 authorized_keys
  5. (optional) Add details top .ssh/config for easy access.

  6. Set up the head node.

     $ sudo swapoff -a
    
     # prerequisite packages
     $ sudo apt-get update && sudo apt-get install -y \
         apt-transport-https ca-certificates curl software-properties-common nginx
     $ sudo apt-get install -y docker.io
    
     # k8s
     $ sudo sh -c "echo 'deb http://apt.kubernetes.io/ kubernetes-xenial main' >> /etc/apt/sources.list.d/kubernetes.list"
     $ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
     $ sudo apt-get update && sudo apt-get install -y \
       kubelet=1.18.2-00 \
       kubeadm=1.18.2-00 \
       kubectl=1.18.2-00
    
     # hold the versions to prevent automatic upgrades leading to incompatibilities
     $ sudo apt-mark hold docker-ce kubelet kubeadm kubectl
    
     # Helm, NFS
     $ curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
     $ sudo apt-get install nfs-common -y
  7. Initialise the head node.

     $ sudo kubeadm init --kubernetes-version 1.18.2 --pod-network-cidr=10.244.0.0/16 | tee clusterInit.out
    
     # get the administration configuration file and move it into user's workspace
     $ mkdir -p $HOME/.kube
     $ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
     $ sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
     # install flannel for networking
     $ wget https://raw.githubusercontent.com/rohinijoshi06/jupyterhub-on-k8s/master/kube-flannel.yaml
     $ sudo kubectl apply -f kube-flannel.yaml

Worker nodes

  1. Spin up VM instance (add to network, router interfaces, ssh key etc.). This will be for one of the worker nodes.

  2. Add instance IP to /etc/hosts on DMZ.

  3. SSH to instance and add new user. Add user to group sudo.

     $ usermod -aG sudo <username>
     $ sudo visudo
    
     # Allow members of group sudo to execute any command
     %sudo   ALL=(ALL:ALL) NOPASSWD:ALL
    
     $ sudo usermod -aG sudo <username>
  4. Add new user's public ssh keys.

     $ sudo su - <username>
     $ mkdir .ssh
     $ vim .ssh/authorized_keys
     $ chmod 600 authorized_keys
  5. (optional) Add details top .ssh/config for easy access.

  6. Set up the worker node.

     $ sudo swapoff -a
    
     # prerequisite packages
     $ sudo apt-get update -y && sudo apt-get install -y curl nfs-common
     $ sudo apt-get install -y docker.io
    
     # k8s
     $ sudo sh -c "echo 'deb http://apt.kubernetes.io/ kubernetes-xenial main' >> /etc/apt/sources.list.d/kubernetes.list"
     $ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
     $ sudo apt-get update && sudo apt-get install -y \
       kubelet=1.18.2-00 \
       kubeadm=1.18.2-00 \
       kubectl=1.18.2-00
    
     # hold the versions to prevent automatic upgrades leading to incompatibilities
     $ sudo apt-mark hold docker-ce kubelet kubeadm kubectl
  7. Join the worker node to the cluster using the command generated on the head's $HOME/clusterInit.out (kubeadm join).

Testing

From the head node:

$ kubectl get nodes

Top